<?php

namespace common\models\project;

use common\helpers\StringHelper;
use Yii;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "hxdc_project_tehuihaofang".
 *
 * @property int $id
 * @property string $title 标题
 * @property int $merchant_id 商户id
 * @property string $shorttitle 简略标题
 * @property string $img_url 简略标题
 * @property int $project_id 关联project项目表id
 * @property string $bianhao 编号/代码
 * @property string $yetai 业态
 * @property string $xianjia 现价
 * @property string $yuanjia 原价
 * @property string $youhui 优惠
 * @property string $tel 抢购电话
 * @property int $jiezhiriqi 截止日期
 * @property string $mianji 面积
 * @property int $sort 排序
 * @property int $created_at 创建时间
 * @property int $updated_at 最后一次更新时间
 * @property int $status 状态[-1删除；0禁用；1启用]
 */
class ProjectTehuihaofang extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hxdc_project_tehuihaofang';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['project_id'], 'required'],
            [['project_id', 'merchant_id', 'sort', 'created_at', 'updated_at', 'status'], 'integer'],
            [['title', 'yetai'], 'string', 'max' => 31],
            [['shorttitle'], 'string', 'max' => 127],
            [['bianhao'], 'string', 'max' => 63],
            [['img_url'], 'string', 'max' => 255],
            [['xianjia', 'youhui', 'tel', 'mianji','yuanjia'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'merchant_id' => '商户 ID',
            'title' => '标题',
            'shorttitle' => '副标题',
            'img_url' => '图片',
            'project_id' => '项目 ID',
            'bianhao' => '编号',
            'yetai' => '业态',
            'xianjia' => '现价',
            'yuanjia' => '原价',
            'youhui' => '优惠',
            'tel' => '抢购电话',
            'jiezhiriqi' => '截止日期',
            'mianji' => '面积',
            'sort' => '排序',
            'created_at' => '创建时间',
            'updated_at' => '最后一次更新时间',
            'status' => '状态',
        ];
    }

    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}
